package com.freeter.modules.cityrecord.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freeter.common.mpextend.parser.ParseWrapper;
import com.freeter.common.provincecity.ProvinceCityCache;
import com.freeter.common.utils.JQPageInfo;
import com.freeter.common.utils.PageInfo;
import com.freeter.common.utils.PageUtils;
import com.freeter.common.utils.R;
import com.freeter.common.validator.ValidatorUtils;
import com.freeter.modules.cityrecord.entity.CityRecordEntity;
import com.freeter.modules.cityrecord.entity.model.CityRecordModel;
import com.freeter.modules.cityrecord.entity.view.CityHotMapView;
import com.freeter.modules.cityrecord.entity.view.CityRecordView;
import com.freeter.modules.cityrecord.service.CityRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 城市记录
 * 后端接口
 *
 * @author xuchen
 * @email 171998110@qq.com
 * @date 2019-04-07 14:01:49
 */
@RestController
@RequestMapping("cityrecord/cityrecord")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CityRecordController {
    @Autowired
    private CityRecordService cityRecordService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("cityrecord:cityrecord:list")
    public PageUtils page(JQPageInfo jqPageInfo, CityRecordModel cityRecord) {
        EntityWrapper<CityRecordEntity> ew = ParseWrapper.parseWrapper(cityRecord);
        PageInfo pageInfo = new PageInfo(jqPageInfo);
        PageUtils page = cityRecordService.queryPage(pageInfo, ew);

        return page;

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("cityrecord:cityrecord:list")
    public R list(CityRecordModel cityRecord) {
        EntityWrapper<CityRecordEntity> ew = ParseWrapper.parseWrapper(cityRecord);
        return R.ok().put("data", cityRecordService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    @RequiresPermissions("cityrecord:cityrecord:info")
    public R query(CityRecordEntity cityRecord) {
        EntityWrapper<CityRecordEntity> ew = ParseWrapper.parseWrapper(cityRecord);
        CityRecordView cityRecordView = cityRecordService.selectView(ew);
        return R.ok("查询城市记录成功").put("data", cityRecordView);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cityrecord:cityrecord:info")
    public R info(@PathVariable("id") Integer id) {
        CityRecordEntity cityRecord = cityRecordService.selectById(id);

        return R.ok().put("cityRecord", cityRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("cityrecord:cityrecord:save")
    public R save(@RequestBody CityRecordEntity cityRecord) {
        ValidatorUtils.validateEntity(cityRecord);
        cityRecordService.insert(cityRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("cityrecord:cityrecord:update")
    public R update(@RequestBody CityRecordEntity cityRecord) {
        ValidatorUtils.validateEntity(cityRecord);
        cityRecordService.updateAllColumnById(cityRecord);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("cityrecord:cityrecord:delete")
    public R delete(@RequestBody Integer[] ids) {
        cityRecordService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 热点城市图
     *
     * @return
     */
    @RequestMapping("/selecthotcitymap")
    public R selectHotCityMap() {
        Map<String, CityHotMapView> hotCityMap = new HashMap<>();
        cityRecordService.selectHotCityMap().forEach(vo -> hotCityMap.put(vo.getProvinceName(), vo));
        return R.ok().put("hotCityMap", hotCityMap);
    }

    /**
     * 根据省份名字获取记录
     *
     * @param provinceName
     * @return
     */
    @RequestMapping("/selectrecordbyprovince/{provinceName}")
    public R selectRecordByProvince(@PathVariable("provinceName") String provinceName) {
        List<CityRecordEntity> cityRecordEntityList = cityRecordService.selectRecordByProvince(provinceName);
        Map<String, List<CityRecordEntity>> map = cityRecordEntityList.stream().collect(Collectors.groupingBy(CityRecordEntity::getCityId));
        List<Map<String, Integer>> list = new ArrayList<>();
        map.forEach((key, value) -> {
            Map<String, Integer> tempMap = new HashMap<>();
            tempMap.put(ProvinceCityCache.getCity(key).getCity(), value.size());
            list.add(tempMap);
        });
        return R.ok().put("cityList", list);
    }

    /**
     * 获取所有省
     *
     * @return
     */
    @RequestMapping("getprovinces")
    @ResponseBody
    public Object getProvinces() {
        return R.ok().put("provinceList", ProvinceCityCache.getAllProvinces());
    }

    /**
     * 根据省获取市
     *
     * @param provinceId
     * @return
     */
    @RequestMapping("getcitiesbyprovinceid")
    @ResponseBody
    public Object getCitiesByProvinceId(String provinceId) {
        return R.ok().put("cityList", ProvinceCityCache.getCitiesByProvinceId(provinceId));
    }

}
